package com.starwish.starwishblog.web.back;

import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.PermissionService;
import com.starwish.starwishblog.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("admin/menu")
@Api(value = "对菜单进行操作")
public class MenuController {

    @Autowired
    PermissionService permissionService;

    @GetMapping("/showMenus")
    @ApiOperation(value = "查询所有菜单")
    public ResultResponse<List<MenuVo>> showMenus(){
        ResultResponse<List<MenuVo>> result = null;
        try {
            List<MenuVo> menus = permissionService.queryAllMenus();
            result = new ResultResponse<>(200,"查询菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询菜单失败");
        }
        return result;
    }

    @GetMapping("/showMenusByName")
    @ApiOperation(value = "根据登录用户查询菜单")
    public ResultResponse<List<MenuVo>> showMenusByName(String name){
        ResultResponse<List<MenuVo>> result = null;
        try {
            List<MenuVo> menuVos = permissionService.queryMenusByName(name);
            result = new ResultResponse<>(200,"ok",menuVos);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败");
        }
        return result;
    }
}
